//
//  SingleRecordResultVC.m
//  record_plugin
//
//  Created by zqj on 2024/9/4.
//
// [NSURL URLWithString:@"https://sample-videos.com/video123/mp4/480/big_buck_bunny_480p_2mb.mp4"]
#import "SingleRecordResultVC.h"
#import "Constant.h"
//zfplayer
#import "ZFPlayer/Classes/AVPlayer/ZFAVPlayerManager.h"
#import "ZFPlayer/Classes/Core/ZFPlayerController.h"
#import "ZFPlayer/Classes/ControlView/ZFPlayerControlView.h"

@interface SingleRecordResultVC ()
@property (weak, nonatomic) IBOutlet UILabel *resultLab;
@property (weak, nonatomic) IBOutlet UIButton *descBtn1;
@property (weak, nonatomic) IBOutlet UIButton *descBtn2;
@property (weak, nonatomic) IBOutlet UILabel *errLab1;
@property (weak, nonatomic) IBOutlet UILabel *errLab2;
@property (weak, nonatomic) IBOutlet UIView *playerContainerView;

@property (weak, nonatomic) IBOutlet UIButton *playBtn;
@property (nonatomic, strong) ZFPlayerController *player;
@property (nonatomic, strong) ZFPlayerControlView *controlView;
@property (nonatomic, strong) NSArray <NSURL *>*assetURLs;

@end

@implementation SingleRecordResultVC

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationItem.hidesBackButton = YES;
}

- (void)dealloc{
    NSLog(@"SingleRecordResultVC ==== dealloc");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}


- (void)setupUI{
    self.title = @"智能播报结果页";
    [self setupPlayer];
    if (self.isFacePass) {
        [self.descBtn1 setSelected:YES];
        self.resultLab.text = @"本次视频下述内容质检通过:";
        self.descBtn1.titleLabel.text = @"视频过程中仅出现投资者本人";
        self.errLab1.hidden = YES;

    }else{
        self.resultLab.text = @"本次视频以下内容质检不通过，建议重新录制视频";
        self.descBtn1.titleLabel.text = @"证件和人脸不一致";
        self.errLab1.text = @"请确保证件上传步骤上传的证件照片和视频录制为同一人";

    }
    if (self.isVoicePass) {
        [self.descBtn2 setSelected:YES];
        self.resultLab.text = @"本次视频下述内容质检通过:";
        self.descBtn2.titleLabel.text = @"请核对视频回答，确认回答无误后点击提交";
        self.errLab2.hidden = YES;

    }else{
        self.resultLab.text = @"本次视频以下内容质检不通过，建议重新录制视频";
        self.errLab2.text = @"按照提示回答，确保声音清晰、回答无误";
        self.descBtn2.titleLabel.text = @"回答不符合要求";

    }
    [self.playerContainerView bringSubviewToFront:self.playBtn];

}


- (void)setupPlayer {
    ZFAVPlayerManager *playerManager = [[ZFAVPlayerManager alloc] init];
    playerManager.shouldAutoPlay = NO;
    /// 播放器相关
    self.player = [ZFPlayerController playerWithPlayerManager:playerManager containerView:self.playerContainerView];
    self.player.controlView = self.controlView;
    
    @zf_weakify(self)
    self.player.orientationDidChanged = ^(ZFPlayerController * _Nonnull player, BOOL isFullScreen) {
   
    };
    /// 播放完成
    self.player.playerDidToEnd = ^(id  _Nonnull asset) {
        @zf_strongify(self)
    };
    self.player.assetURLs = self.assetURLs;
   [self.player playTheIndex:0];
    [self.controlView showTitle:@"视频回放" coverURLString:nil placeholderImage:self.placeholderImg fullScreenMode:ZFFullScreenModeAutomatic];

  
}

- (IBAction)playClicked:(UIButton *)sender {
    sender.hidden = YES;
    [self.player.currentPlayerManager play];
}

- (ZFPlayerControlView *)controlView {
    if (!_controlView) {
        _controlView = [ZFPlayerControlView new];
        _controlView.prepareShowLoading = YES;
        _controlView.prepareShowControlView = NO;
        _controlView.showCustomStatusBar = YES;
    }
    return _controlView;
}

- (NSArray<NSURL *> *)assetURLs {
    if (!_assetURLs) {
        _assetURLs = @[
            [NSURL URLWithString:self.videoUrl]];
    }
    return _assetURLs;
}

- (IBAction)reRecordClicked:(UIButton *)sender {
    if (self.remakeblock) {
        self.remakeblock();
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)commitRecordClicked:(UIButton *)sender {
    if (self.block) {
        self.block(self.videoUrl);
    }
    NSArray *vcs = self.navigationController.viewControllers;
    NSLog(@"当前栈有%ld个VC",vcs.count);
    [self.navigationController popToViewController:vcs[vcs.count - 3] animated:YES];
    
}

-(UIImage *)getVideoPreviewImage:(NSString *)videoURL{
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL: [NSURL fileURLWithPath:videoURL] options:nil];
    AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    gen.appliesPreferredTrackTransform = YES;
    // 获取第一帧图像
    CMTime time = CMTimeMake(1, 1); // 获取 0 秒时的帧
    NSError *error = nil;
    CMTime actualTime;
    CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
    UIImage *thumb =[[UIImage alloc] initWithCGImage:image];
    CGImageRelease(image);
    return thumb;
}



@end
